package com.cci.kangdao.dao;

import java.util.List;
import java.util.Map;

import com.cci.kangdao.dao.model.UserT;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;

import com.cci.kangdao.dao.model.PositionUserT;

/**
 * 岗位用户类dao
 *
 */
@MapperScan
public interface PositionUserTDao {
	
	
	
	//根据服务站id和服务渠道id获取区域服务经理手机号
	@Select("select distinct b.UserPhone,d.ID as UserID\n" +
			"from PositionUserServiceStation_T a\n" +
			"inner join PositionUser_T b on b.ID = a.PositionUserID\n" +
			"inner join Position_T c on b.PositionID = c.ID\n" +
			"left join User_T d on d.Phone = b.UserPhone\n" +
			"where a.ServiceStationID=#{ServiceStationID} and a.CompanyID = #{CompanyID} and c.Level = 1 and a.Flag=0 and b.Flag=0 and c.Flag=0 and d.Flag=0")
	public List<PositionUserT> getPostUserTListByServiceStationIdCompanyId(Map<String, Object> map);


	//根据服务站id和服务渠道id获取区域服务经理手机号
	@Select("select distinct b.UserPhone,d.ID as UserID\n" +
			"from PositionUserServiceStation_T a\n" +
			"inner join PositionUser_T b on b.ID = a.PositionUserID\n" +
			"inner join Position_T c on b.PositionID = c.ID\n" +
			"left join User_T d on d.Phone = b.UserPhone\n" +
			"where a.ServiceStationID=#{ServiceStationID} and a.CompanyID = #{CompanyID} and c.Level = 2 and a.Flag=0 and b.Flag=0 and c.Flag=0 and d.Flag=0")
	public List<PositionUserT> getManager(Map<String, Object> map);

	@Select(" select distinct b.UserPhone,d.ID as UserID" +
			" from PositionUserServiceStation_T a" +
			" inner join PositionUser_T b on b.ID = a.PositionUserID" +
			" inner join ServiceStationRelation_T ssr on a.ServiceStationID = ssr.ParentID and ssr.Flag = 0" +
			" inner join Position_T c on b.PositionID = c.ID" +
			" left join User_T d on d.Phone = b.UserPhone" +
			" where ssr.ChildID=#{ServiceStationID} and a.CompanyID = #{CompanyID} and c.Level = 1 and a.Flag=0 and b.Flag=0 and c.Flag=0 and d.Flag=0")
	public List<PositionUserT> getPostUserTListByChildStationIdCompanyId(Map<String, Object> map);
	
	//根据工单id获取DBU指定区域工程师信息
	@Select("select top 1 a.* from RecommendThreeStationForDBU_T a where a.OrderId = #{orderId} and a.Flag = 0")
	Map<String,Object> getDBUAreaManager(@Param("orderId") long orderId);

	/**
	 * 查询渠道服务商的区域服务经理、区域工程师用户信息
	 * @param companyId
	 * @param serviceStationId
	 * @return
	 */
	@Select("select distinct ut.* from PositionUserServiceStation_T puss\n" +
			"inner join PositionUser_T pu on puss.PositionUserID = pu.ID\n" +
			"inner join User_T ut on ut.Phone = pu.UserPhone\n" +
			"inner join Position_T po on pu.PositionID = po.ID\n" +
			"where ut.Flag = 0 and puss.Flag = 0 and po.Flag = 0 and pu.Flag = 0 and po.Level = 1 and puss.CompanyID = #{companyId} and puss.ServiceStationID = #{serviceStationId}")
	List<UserT> getUserListByServiceStationIdCompanyId(@Param("companyId") Long companyId,@Param("serviceStationId") Long serviceStationId);

	@Select("select distinct ut.* from PositionUserServiceStation_T puss\n" +
			"inner join PositionUser_T pu on puss.PositionUserID = pu.ID\n" +
			"inner join User_T ut on ut.Phone = pu.UserPhone\n" +
			"inner join Position_T po on pu.PositionID = po.ID\n" +
			"where ut.Flag = 0 and puss.Flag = 0 and po.Flag = 0 and pu.Flag = 0 and po.Level = 1 and puss.CompanyID = #{companyId}")
	List<UserT> getUserListByCompanyId(@Param("companyId") Long companyId);


	List<Map<String,Object>> getUserListByCondition(@Param("companyId") Long companyId,@Param("serviceStationId") Long serviceStationId,@Param("level") Integer level);
	@Select("select distinct p.* from Position_T p " +
			"    inner join PositionUser_T pu on pu.PositionID = p.ID" +
			"    where p.Flag = 0 and pu.Flag = 0 and pu.UserPhone = #{engineerPhone} and p.Creator = #{channelId} and p.CrmPositionName = #{crmPositionName} and pu.Marketing = #{marketType}")
	Map<String,Object> getPositionByPhone(@Param("engineerPhone") String engineerPhone ,@Param("channelId") Long channelId,@Param("crmPositionName") String crmPositionName,@Param("marketType") String marketType);
	
	@Select("select top 1 b.CrmPositionName from PositionUser_T a join Position_T b on a.PositionID = b.ID where a.UserPhone = #{phone} and a.Flag = 0 and b.Flag = 0")
	String getCrmPositionNameByPhone(@Param("phone") String phone);

    List<PositionUserT> getListbyCrm(List<String> list);
}
